package domain.service;

import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import org.datanucleus.store.mapped.expression.ScalarExpression.IllegalOperationException;

import domain.model.Domain;

public class Utils {

	public static String textCrop(String content,String begin, String end)
	{
		 int start = content.indexOf(begin)+begin.length();
		    
		 int finish = content.indexOf(end,start); 
		 return content.substring(start, finish);
		    
		
	}
	public static String convertToString(List list)
	{
		
		String str="";
		for(Object jString : list)
		{
			str+=jString.toString()+",";
			
		}
		return str;
	}
	public static int checkLengthDomain(String domain)
	{
		try
        {
			ResourceBundle resource = ResourceBundle.getBundle("language", new Locale("en"));
			String re_domain = domain;
			re_domain = re_domain.replaceAll("com[.]{1}[a-zA-Z]+$", "");

        	String[] check = resource.getString("domain").split(" ");
        	for(int i=0;i<check.length;i++)
        	{
        		re_domain = re_domain.replaceAll(check[i], "");
        	}

	        
	        if(re_domain.length() != domain.length())
	        {
	        	return re_domain.replaceAll("[.]+$", "").length();
	        } else {
	        	return re_domain.replaceAll("[.]{1}[a-zA-Z]+$", "").length();
	        }
        }
        catch (IllegalOperationException ioe)
	    {
	        ioe.printStackTrace();
	    }
		return 0;
	}
	public static String returnDomain(String domain)
	{
		try
        {
			String re_domain = domain;
			re_domain = re_domain.replaceAll("com[.]{1}[a-zA-Z]+$", "");
			ResourceBundle resource = ResourceBundle.getBundle("language", new Locale("en"));
        	String[] check = resource.getString("domain").split(" ");
        	for(int i=0;i<check.length;i++)
        	{
        		re_domain = re_domain.replaceAll(check[i], "");
        	}

	        
	        if(re_domain.length() != domain.length())
	        {
	        	return re_domain.replaceAll("[.]+$", "").replaceAll("^[a-zA-Z_\\-0-9]+[.]{1}", "");
	        } else {
	        	return re_domain.replaceAll("[.]{1}[a-zA-Z]+$", "").replaceAll("^[a-zA-Z_\\-0-9]+[.]{1}", "");
	        }
        }
        catch (IllegalOperationException ioe)
	    {
	        ioe.printStackTrace();
	    }
		return domain;
	}
	public static String returnSubDomain(String domain)
	{
		try
        {
			String re_domain = domain;
			re_domain = re_domain.replaceAll("com[.]{1}[a-zA-Z]+$", "");
			ResourceBundle resource = ResourceBundle.getBundle("language", new Locale("en"));
        	String[] check = resource.getString("domain").split(" ");
        	for(int i=0;i<check.length;i++)
        	{
        		re_domain = re_domain.replaceAll(check[i], "");
        	}

	        if(re_domain.length() != domain.length())
	        {
	        	re_domain = re_domain.replaceAll("[.]+$", "");
	        	String str = re_domain.replaceAll("^[a-zA-Z_\\-0-9]+[.]{1}", "");
	        	if(str.length() != re_domain.length())
	        	{
	        		return " is sub domain of <strong>"+domain.replaceAll("^[a-zA-Z_\\-0-9]+[.]{1}", "")+"</strong>";
	        	} else {
	        		return " is top level domain";
	        	}	        	
	        } else {
	        	re_domain = re_domain.replaceAll("[.]{1}[a-zA-Z]+$", "");
	        	String str = re_domain.replaceAll("^[a-zA-Z_\\-0-9]+[.]{1}", "");
	        	if(str.length() != re_domain.length())
	        	{
	        		return " is sub domain of <strong>"+domain.replaceAll("^[a-zA-Z_\\-0-9]+[.]{1}", "")+"</strong>";
	        	} else {
	        		return " is top level domain";
	        	}	
	        }
        }
        catch (IllegalOperationException ioe)
	    {
	        ioe.printStackTrace();
	    }
		return domain;
	}
	public static String convert2DomainString(List<Domain> list)
	{		
		String str="";
		for(int i=0;i<list.size();i++)
		{
			str+="<a href=\"/domain/"+list.get(i).getName()+"\">"+list.get(i).getName()+"</a> , ";
			if(i >= 14)break;	
		}
		return str;
	}
	public static String cutString(String str,int count)
	{		
		String string = str;
		if(str.length() > count)
		{
			string = str.substring(0,count);
		}
		return string;
	}
	
}
